package com.qpl.depression.cms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.cms.common.ServerResponse;
import com.qpl.depression.cms.entity.Category;
import com.qpl.depression.cms.entity.Paper;
import com.qpl.depression.cms.service.CategoryService;
import com.qpl.depression.cms.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class PagerController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 试题详情
     *
     * @return
     */
    @RequestMapping("/paper/detail")
    public String paperDetail(Long categoryId, ModelMap map) {
        Category category = categoryService.getById(categoryId);
        map.addAttribute("category", category);
        List<Paper> papers = paperService.list(Wrappers.<Paper>lambdaQuery().eq(Paper::getCategoryId, categoryId).eq(Paper::getStatus, 1));
        map.addAttribute("papers", papers);
        return "paperDetail";
    }

    /**
     * 试题添加
     *
     * @return
     */
    @PostMapping("/paper/addPaper")
    @ResponseBody
    public ServerResponse paperAdd(Paper paper) {
        return paperService.save(paper) ? ServerResponse.createBySuccessMessage("success") : ServerResponse.createBySuccessMessage("fail");
    }

    /**
     * 试题删除
     *
     * @return
     */
    @GetMapping("/paper/deleteById")
    @ResponseBody
    public ServerResponse deleteById(int id) {
        return paperService.removeById(id) ? ServerResponse.createBySuccessMessage("success") : ServerResponse.createBySuccessMessage("fail");
    }

    /**
     * 试题更新
     *
     * @return
     */
    @PostMapping("/paper/updatePaper")
    @ResponseBody
    public ServerResponse updatePaper(Paper paper) {
        return paperService.updateById(paper) ? ServerResponse.createBySuccessMessage("success") : ServerResponse.createBySuccessMessage("fail");
    }
}
